package com.codeyang.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述: 接管ali短信发送自动配置
 *
 * @author CodeYang_Site
 * @version 2021/5/30 13:17
 */
@Configuration
@EnableConfigurationProperties(value = AliSmsProperties.class)
public class AliSmsAutoConfiguration {


    @Autowired
    private AliSmsProperties aliSmsProperties;

    /**
     * 创建发送短信的客户端
     *
     * @return
     */
    @Bean
    public IAcsClient acsClient() {
        IClientProfile profile = DefaultProfile.getProfile(
                aliSmsProperties.getRegion(),
                aliSmsProperties.getAccessKeyId(),
                aliSmsProperties.getAccessKeySecret());
        try {
            DefaultProfile.addEndpoint(
                    aliSmsProperties.getRegion(),
                    aliSmsProperties.getRegion(),
                    aliSmsProperties.getProduct(),
                    aliSmsProperties.getDomain());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new DefaultAcsClient(profile);
    }
}
